package com.itheima.thread.utils.concurrenthashmap.demo03;

import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class ConcurrentHashMapDemo03 {

    public static void main(String[] args) {

        for (int i1 = 0; i1 < 30; i1++) {

            ConcurrentHashMap<String, String> concurrentHashMap = new ConcurrentHashMap<>();

            Thread t1 = new Thread() {
                @Override
                public void run() {
                    for (int i = 1; i < 25; i++) {
                        concurrentHashMap.put(String.valueOf(i), String.valueOf(i));
                    }
                }
            };

            Thread t2 = new Thread() {
                @Override
                public void run() {
                    for (int i = 25; i < 51; i++) {
                        concurrentHashMap.put(String.valueOf(i), String.valueOf(i));
                    }
                }
            };

            t1.start();
            t2.start();

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            for (int i = 1; i < 51; i++) {
                if (!String.valueOf(i).equals(concurrentHashMap.get(String.valueOf(i)))) {
                    System.out.println(String.valueOf(i) + ":" + concurrentHashMap.get(String.valueOf(i)));
                }
            }
        }
    }
}
